package com.briup.bookstore.service.impl;

import com.briup.bookstore.common.model.entity.Book;
import com.briup.bookstore.common.model.param.PageParam;
import com.briup.bookstore.dao.BookDao;
import com.briup.bookstore.service.BookService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class BookServiceImpl implements BookService {

	private final BookDao bookDao;

    @Override
    public PageInfo<Book> pageQueryByClause(PageParam pageParam, Book bookParam) {
        /* 开始分页 */
        PageHelper.startPage(pageParam.pageNum(), pageParam.pageSize());
        /* 调用Dao层查询 */
        List<Book> books = bookDao.selectByClause(bookParam);
        /* 封装为分页对象 */
        return PageInfo.of(books);
    }

	/**
	 * 添加图书
	 * @param bookParam 图书添加的基础数据实体
	 */
	@Override
	public void add(Book bookParam) {
	    /* 设置图书默认状态（0-上架，1-下架） */
	    bookParam.setStatus(0);
        /* 调用Dao层执行插入 */
        bookDao.insert(bookParam);
    }

    @Override
    public void update(Book bookParam) {
        bookDao.update(bookParam);
    }

    @Override
    public Book getById(Integer id) {
        return bookDao.selectById(id);
    }

    @Override
    public void delete(List<Integer> ids) {
        bookDao.delete(ids);
    }

    @Override
    public void update(Integer id, Integer status) {
        Book book = getById(id);
        book.setStatus(status);
        update(book);
    }


}
